package com.demo.client;

import com.alibaba.fastjson.JSONObject;
import com.demo.client.pojo.com.demo.pojo.Message;
import com.demo.client.pojo.com.demo.pojo.User;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/**
 * @author:谢君臣
 * @Date:2021/7/25-19:39
 * @version:1.0
 * @function:
 */
public class Application {
    public static void main(String[] args) {
        Client client = new Client();
        new Thread(client).start();
    }
}
class Client implements Runnable{
    private static int port = 8888;
    @Override
    public void run(){
        SocketChannel channel = null;
        try {
            channel = SocketChannel.open(new InetSocketAddress(port));
            channel.configureBlocking(false);
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            Scanner in = new Scanner(System.in);
            String data_out = in.nextLine();
            Message message = new Message();
            message.setId(1);
            message.setTime("2021-7-25");
            message.setSend(new User(1001,"张三","127.0.0.1"));
            message.setReceive(new User(1002,"李四","127.0.0.1"));
            while (!data_out.equals("-1")){
                message.setMsg(data_out);
                byteBuffer.put(JSONObject.toJSONBytes(message));
                byteBuffer.flip();
                channel.write(byteBuffer);
                byteBuffer.clear();
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                channel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

//    @Override
//    public void run() {
//        try {
//            Socket socket = new Socket(inetAddress, port);
//            OutputStream outputStream = socket.getOutputStream();
//            System.out.print("请输入通信内容：");
//            Scanner in = new Scanner(System.in);
//            String data_out = in.nextLine();
//            in.close();
//            Message message = new Message();
//            message.setId(1);
//            message.setMsg(data_out);
//            message.setTime("2021-7-25");
//            message.setSend(new User(1001,"张三","127.0.0.1"));
//            message.setReceive(new User(1002,"李四","127.0.0.1"));
//            outputStream.write(JSONObject.toJSONBytes(message));
//            socket.shutdownOutput(); // 结束输出
//            StringBuilder stringBuilder = new StringBuilder();
//            byte[] data = new byte[1024];
//            int length = 0;
//            InputStream inputStream = socket.getInputStream();
//            while ((length = inputStream.read(data))!=-1)
//            {
//                stringBuilder.append(new String(data,0,length));
//            }
//            System.out.println("client端==我收到的内容是：" +stringBuilder);
//            inputStream.close();
//            outputStream.close();
//            socket.close();
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }
}